"""
file: test_pytest.py
Author: 城下秋草
Date: 2024/12/16
Desc: pytest测试示例
"""
import pytest
from calculator import add, div, sub, multiple

@pytest.fixture
def env():
    try:
        print("\r\n=========fixture的前置")
        # a = 4/0
        yield
    finally:
        print("\r\n========fixture的后置处理")

@pytest.fixture(autouse=True,scope='session',name='db')
def db_connect(request):
    def clean():
        print("\r\n+++++数据库清理...")

    request.addfinalizer(clean)

    msg = "数据库的初始化。。。"
    status = "connected"
    print(msg, status)
    return status

def test_add1(env):
    print("this is a add test!")
    assert add(4, 3) == 7

@pytest.mark.smoke
def test_div1():
    assert  div(4,2) == 2
    with pytest.raises(ZeroDivisionError):
        div(4,0)

@pytest.mark.usefixtures('env')
class CheckResult:
    def test_sub(self):
        assert sub(4,2) == 2

    def test_db(self,db):
        assert db == "connected"

if __name__=="__main__":
    pytest.main()